package com.czk.gulimall.product.app;

import com.czk.common.utils.PageUtils;
import com.czk.common.utils.R;
import com.czk.common.valid.AddGroup;
import com.czk.common.valid.UpdateGroup;
import com.czk.common.valid.UpdateStatusGroup;
import com.czk.gulimall.product.entity.BrandEntity;
import com.czk.gulimall.product.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 品牌
 *
 * @author chenzhangkun
 * @email 1052031630@qq.com
 * @date 2021-12-07 21:29:05
 */
@RestController
@RequestMapping("product/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:brand:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = brandService.queryPage(params);

        return R.ok().put("page", page);
    }

    @GetMapping("/infos")
    public R info(@RequestParam("brandIds") List<Long> brandIds) {
        List<BrandEntity> brandEntities = brandService.getBrandByIds(brandIds);
        return R.ok().put("brand", brandEntities);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{brandId}")
    //@RequiresPermissions("product:brand:info")
    public R info(@PathVariable("brandId") Long brandId) {
        BrandEntity brand = brandService.getById(brandId);

        return R.ok().put("brand", brand);
    }

    /**
     * 保存
     * Validated({AddGroup.class})可以指定分组校验
     *
     * @param brand
     * @param bindingResult 封装校验结果
     * @return
     * @Valid告诉springmvc需要校验
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:brand:save")
    public R save(/*@Valid*/ @Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult bindingResult*/) {
/*        if (bindingResult.hasErrors()){
            Map<String,Object> map=new HashMap<>();
            // 获取错误结果
            bindingResult.getFieldErrors().forEach(fieldError -> {
                // 获取到错误提示，自定义的也可以是默认的
                String defaultMessage = fieldError.getDefaultMessage();
                // 获取错误的属性
                String field = fieldError.getField();
                map.put(field,defaultMessage);
            });
            return R.error(400,"提交的数据不合法").put("data",map);
        }else {

        }*/
        brandService.save(brand);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:brand:update")
    public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateDetail(brand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update/status")
    //@RequiresPermissions("product:brand:update")
    public R updateStaus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
        brandService.updateById(brand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:brand:delete")
    public R delete(@RequestBody Long[] brandIds) {
        brandService.removeByIds(Arrays.asList(brandIds));

        return R.ok();
    }

}
